using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Cou;
using net_work.IService.Cou;

namespace net_work.Controllers.Cou;

[Route("api/[controller]")]
[ApiController]
public class ScheduleController(ILogger<ScheduleController> logger, IScheduleService scheduleService) : ControllerBase
{
    /// <summary>
    /// 获取排课列表
    /// </summary>
    /// <param name="request">分页请求实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("list")]
    [Authorize]
    public async Task<IActionResult> ListSchedules([FromBody] PagedRequest request)
    {
        logger.LogInformation("获取排课列表");
        var result = await scheduleService.GetSchedulesPagedAsync(request);
        return AjaxResult.Success(result);
    }

    /// <summary>
    /// 获取排课信息
    /// </summary>
    /// <param name="scheduleId">排课ID</param>
    /// <returns>操作结果</returns>
    [HttpGet("get")]
    [Authorize]
    public async Task<IActionResult> GetScheduleById([FromQuery] long scheduleId)
    {
        logger.LogInformation("获取排课信息");
        return await scheduleService.SelectByIdAsync(scheduleId) is { } schedule
            ? AjaxResult.Success(schedule)
            : AjaxResult.Error();
    }

    /// <summary>
    /// 新增排课
    /// </summary>
    /// <param name="scheduleAddRequest">新增排课实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("add")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> AddSchedule([FromBody] ScheduleAddRequest scheduleAddRequest)
    {
        logger.LogInformation("新增排课");
        var scheduleId = await scheduleService.InsertSchedule(scheduleAddRequest);
        return scheduleId > 0
            ? AjaxResult.Success(new { scheduleId })
            : AjaxResult.Error();
    }

    /// <summary>
    /// 修改排课
    /// </summary>
    /// <param name="scheduleUpdateRequest">修改排课实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("update")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> UpdateSchedule([FromBody] ScheduleUpdateRequest scheduleUpdateRequest)
    {
        logger.LogInformation("修改排课");
        return await scheduleService.UpdateSchedule(scheduleUpdateRequest) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }

    /// <summary>
    /// 删除排课
    /// </summary>
    /// <param name="ids">排课ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("delete")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> DeleteSchedule([FromBody] List<long> ids)
    {
        logger.LogInformation("删除排课");
        return await scheduleService.DeleteScheduleByIds(ids) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }
}